package com.nfdw.project.web;
import com.alibaba.fastjson.JSONObject;
import com.nfdw.project.core.Result;
import com.nfdw.project.core.ResultGenerator;
import com.nfdw.project.model.MkDiPowerTradeSettleInfo;
import com.nfdw.project.service.MkDiPowerTradeSettleInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* Created by CodeGenerator on 2024/05/22.
*/
@RestController
@RequestMapping("/mk/di/power/trade/settle/info")
public class MkDiPowerTradeSettleInfoController {
    @Resource
    private MkDiPowerTradeSettleInfoService mkDiPowerTradeSettleInfoService;

    @PostMapping("/add")
    public Result add(MkDiPowerTradeSettleInfo mkDiPowerTradeSettleInfo) {
        mkDiPowerTradeSettleInfoService.save(mkDiPowerTradeSettleInfo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        mkDiPowerTradeSettleInfoService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(MkDiPowerTradeSettleInfo mkDiPowerTradeSettleInfo) {
        mkDiPowerTradeSettleInfoService.update(mkDiPowerTradeSettleInfo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        MkDiPowerTradeSettleInfo mkDiPowerTradeSettleInfo = mkDiPowerTradeSettleInfoService.findById(id);
        return ResultGenerator.genSuccessResult(mkDiPowerTradeSettleInfo);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<MkDiPowerTradeSettleInfo> list = mkDiPowerTradeSettleInfoService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * 发电企业结算情况
     * @return
     */
    @PostMapping("/jsqk")
    public Result jsqk(MkDiPowerTradeSettleInfo mkDiPowerTradeSettleInfo) {
        MkDiPowerTradeSettleInfo mdptsi = mkDiPowerTradeSettleInfoService.findMdptsiJsqk(mkDiPowerTradeSettleInfo);
        return ResultGenerator.genSuccessResult(mdptsi);
    }

    /**
     * 发电企业结算情况
     * @return
     */
    @PostMapping("/jsfx")
    public Result jsfx(MkDiPowerTradeSettleInfo mkDiPowerTradeSettleInfo) {
        List<JSONObject> result = mkDiPowerTradeSettleInfoService.selectMdptsiJsfx(mkDiPowerTradeSettleInfo);
        //组装图表数据
        ArrayList<Object> xAxisData = new ArrayList<>();
        JSONObject seriesData = new JSONObject();

        ArrayList<Object> monTradeFeeList = new ArrayList<>();
        ArrayList<Object> monTradePowerList = new ArrayList<>();
        ArrayList<Object> monTradePriceList = new ArrayList<>();
        ArrayList<Object> noBalanceFeeList = new ArrayList<>();
        ArrayList<Object> yearTradeFeeList = new ArrayList<>();
        ArrayList<Object> yearTradePowerList = new ArrayList<>();
        ArrayList<Object> yearTradePriceList = new ArrayList<>();
        ArrayList<Object> yearNobalanceFeeList = new ArrayList<>();

        for (JSONObject jsonObject : result) {
            xAxisData.add(jsonObject.get("itemsName"));
            JSONObject info = jsonObject.getJSONObject("data");

            monTradeFeeList.add(info.get("monTradeFee"));
            monTradePowerList.add(info.get("monTradePower"));
            monTradePriceList.add(info.get("monTradePrice"));
            noBalanceFeeList.add(info.get("noBalanceFee"));

            yearTradeFeeList.add(info.get("yearTradeFee"));
            yearTradePowerList.add(info.get("yearTradePower"));
            yearTradePriceList.add(info.get("yearTradePrice"));
            yearNobalanceFeeList.add(info.get("yearNobalanceFee"));
        }
        seriesData.put("monTradeFeeList",monTradeFeeList);
        seriesData.put("monTradePowerList",monTradePowerList);
        seriesData.put("monTradePriceList",monTradePriceList);
        seriesData.put("noBalanceFeeList",noBalanceFeeList);

        seriesData.put("yearTradeFeeList",yearTradeFeeList);
        seriesData.put("yearTradePowerList",yearTradePowerList);
        seriesData.put("yearTradePriceList",yearTradePriceList);
        seriesData.put("yearNobalanceFeeList",yearNobalanceFeeList);

        //组装好的图表数据
        JSONObject data = new JSONObject();
        data.put("xAxisData",xAxisData);
        data.put("seriesData",seriesData);

        return ResultGenerator.genSuccessResult(data);
    }



    /**
     * 统计测试数据
     * @return
     */
    @GetMapping("/getData")
    public Result list() {
        JSONObject data = new JSONObject();
        ArrayList<String> categories = new ArrayList<>();
        categories.add("A");
        categories.add("B");
        categories.add("C");

        ArrayList<Integer> values  = new ArrayList<>();
        values.add(10);
        values.add(20);
        values.add(30);
        data.put("categories",categories);
        data.put("values",values);
        return ResultGenerator.genSuccessResult(data);
    }


}
